﻿/// <reference path="jquery-1.7.2.js" />
/// <reference path="moment.js" />
/// <reference path="querybuilder.js" />

var queryParams =
       {
           "EntityName":"Person",
           "Filters": {
               "LogicalOperator": "Or",
               "FilterParams": [
                   { "Name": "Age", "Operator": "IsGreaterThan", "Value": 25, "DisplayName": "Возраст" },
                   { "Name": "Age", "Operator": "IsLessThan", "Value": 15, "DisplayName": "Возраст" }
               ],
               "JsonFilterManagers": [{
                   "LogicalOperator": "And",
                   "FilterParams": [
                       { "Name": "Age", "Operator": "IsEqualTo", "Value": 22, "DisplayName": "Возраст" },
                       { "Name": "Age", "Operator": "IsLessThan", "Value": 75, "DisplayName": "Возраст" }
                   ],
                   "JsonFilterManagers": []
               }]
           },
           "Sorters": {
               "SortParams": [{ "Name": "Age", "Sort": "Descending", "DisplayName":"Возраст" }]
           },
           "Groups": {
               "GroupParams": [{ "FieldName": "Age", "DisplayName":"Возраст" }],
               "FieldNameForCount": "Weight"
           }
       }

//var queryParams2 =
//       {
//           "EntityName": "Person2",
//           "Filters": {
//               "LogicalOperator": "And",
//               "FilterParams": [
//                   { "Name": "Country", "Operator": "IsEqual", "Value": "Russia", "DisplayName": "Страна" }                   
//               ],
//               "JsonFilterManagers": []
//           },
//           "Sorters": {
//               "SortParams": [{ "Name": "Age", "Sort": "Descending", "DisplayName": "Возраст" }]
//           },
//           "Groups": {
//               "GroupParams": [{ "FieldName": "Age", "DisplayName": "Возраст" }],
//               "FieldNameForCount": "Weight"
//           }
//       }

$(function () {
    //-----------------------------------
    //loadfields();
    loadpersons();
    //$('input[type="submit"]').click(function () {
    //    loadpersons();
    //});
    //-----------------------------------

    $('#queryBuilder').queryBuilder(queryParams, {
        fieldsUrl: '/home/personfields/'
    });

    //$('#queryBuilder2').queryBuilder(queryParams2, {
        
    //});

   

});

function loadpersons() {
    var name = $('#fieldname').val();
    var filter = $('#filtername').val();
    var value = $('#Value').val();
    //var obj = { 'Name': name, 'Operator': filter, 'Value': value };
    var obj = queryParams;
    $.ajax({
        url: '/home/loadpersons/',
        data: { "jsonParam": JSON.stringify(obj) },
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            $("#result tr:not(:first)").remove();
            $.each(result, function (index, item) {
                $('#result tr:last').after('<tr><td>' + item.Name +
                    '</td><td>' + moment(item.MemberDate).format('DD.MM.YYYY') +
                    '</td><td>' + item.Age +
                    '</td><td>' + item.Country +
                    '</td><td>' + item.Description +
                    '</td><td>' + item.Gender +
                    '</td><td>' + item.IsMember +
                    '</td><td>' + item.Weight + '</td></tr>');
            });
        },
        error: function (jqXHR, textStatus) {
            $("#status").append($("<span>" + textStatus + "</span>"));
        }
    });
}
function loadfields() {
    $.ajax({
        url: '/home/personfields/',
        data: null,
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            if (result) {
                $('#fieldname').append($('<option value="-1" type="-1">--- не выбрано ---</option>'));
                $.each(result, function (index, item) {
                    $('#fieldname').append($('<option value="' + item.Name + '" type="' + item.FilterType + '">' + item.DisplayName + '</option>'));
                });
                $('#fieldname').change(function () {
                    var type = $(this).find('option:selected').attr('type');
                    changeoperator(type);
                });
            }
        },
        error: function (jqXHR, textStatus) {
            $("#status").append($("<span>" + textStatus + "</span>"));
        }
    });
}
function changeoperator(type) {
    $('#filtername').find('option').remove();
    $.ajax({
        url: '/home/changeoperators/',
        data: { "type": type },
        type: 'GET',
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success: function (result) {
            if (result) {
                var fields = $('#filtername')[0];
                fields.add(new Option('--- не выбрано ---', ''));
                $.each(result, function (index, item) {
                    fields.add(new Option(item.DisplayName, item.Name));
                });
            }
        },
        error: function (jqXHR, textStatus) {
            $("#status").append($("<span>" + textStatus + "</span>"));
        }
    });
}

